<?
if( !$PARAPARA )
    die("Do not call me");

$dbl = $PARAPARA->dblayer;
$form_opts = array(
    'email'        => '',
    'screen_name'  => '',
    'pwd'          => '',

    'err_text'     => ''
);


echo "<div id=\"para_register_container\">\n";
echo "<div id=\"para_register_caption\">Register Account</div>\n";
echo "<div id=\"para_register_console\">\n";

// activation link action 
if($_GET['act_code'] && $_GET['uid'] && is_numeric($_GET['uid']) ) {
    $status = $dbl->account_confirm($_GET['uid'], $_GET['act_code']);

    echo "Confirmation status=$status";
}

// post action
elseif($_POST['form']) {
    $email       = trim( $_POST['email'] );
    $screen_name = trim( $_POST['screen_name'] );
    $pwd1        = trim( $_POST['pwd1'] );
    $pwd2        = trim( $_POST['pwd2'] );
    $has_errors  = false;

    if( !Helpers::validate_email( $email ) ) {
        $form_opts['email_err'] = true;
        $form_opts['err_text']  = 'Invalid email address';
    }

    elseif( !Helpers::validate_screen_name( $screen_name ) ) {
        $form_opts['screen_name_err'] = true;
        $form_opts['err_text']        = 'Invalid screen name';
    }

    elseif( $pwd1 != $pwd2 ) {
        $form_opts['pwd_err']  = true;
        $form_opts['err_text'] = 'The two passwords do not match';
    }

    elseif( !Helpers::validate_pwd( $pwd1 ) ) {
        $form_opts['pwd_err']   = true;
        $form_opts['err_text']  = 'Invalid password';
    }

    if( $form_opts['err_text'] ) {
        $form_opts['pwd']         = $pwd1;
        $form_opts['screen_name'] = $screen_name;
        $form_opts['email']       = $email;

        register_form( $form_opts );
    }
    else {

        $retval = $dbl->account_register( $screen_name, $email, $pwd_md5 );
        if(is_object($retval)) {
            $uid      = $retval->uid;
            $act_code = $retval->act_code;

            $retval = $PARAPARA->mail_confirmation( $email, $uid, $act_code );

            echo "Going to confirm the account now...\n";

            echo "Allright";
        }
        else {
            echo "Error occurred: $retval";
        }
    }
}

// show form
else {
    register_form( $form_opts );
}

echo "</div>";
echo "</div>";


function regform_textfield( $name, $size, $value ) {
    $safe_val = htmlspecialchars( $value );
    return 
        "<input type=\"text\" class=\"para_text_input\" id=\"para_register_$name\" ". 
        "name=\"$name\" value=\"$safe_val\" />";
}

function regform_pwdfield( $name, $size, $value ) {
    $safe_val = htmlspecialchars( $value );
    return 
        "<input type=\"password\" class=\"para_text_input\" id=\"para_register_$name\" ". 
        "name=\"$name\" value=\"$safe_val\" />";
}


function regform_row( $label, $value, $is_error ) {
   echo "<tr>\n";
   echo "<td class=\"para_register_label\">$label</td>\n";
   if($is_error) 
       echo "<td class=\"para_register_value\">$value&lt;--</td>\n";
   else
       echo "<td class=\"para_register_value\">$value</td>\n";
   echo "</tr>\n";
}

function register_form( $opts ) {


    echo "<table class=\"para_register_table\">\n";
    echo "<form id=\"para_register_form\" action=\"?sec=register\" method=\"post\"/>";
    echo "<input type=\"hidden\" name=\"form\" value=\"1\" />";
    echo "<tbody>\n";

     if($opts['err_text'] )
         echo "<tr>\n",
             "<td class=\"para_register_form_errmsg\" colspan=\"2\">",
             $opts['err_text'],
             "</td>\n",
             "</tr>\n";

    regform_row('e-mail address:', 
                regform_textfield('email', 50, $opts['email']), 
                $opts['email_err']
        );
    regform_row('nickname:',
                regform_textfield('screen_name', 30, $opts['screen_name']), 
                $opts['screen_name_err']
        );
    regform_row('password:', 
                regform_pwdfield('pwd1', 15, ''),
                $opts['pwd_err']
        );
    regform_row('password:',
                regform_pwdfield('pwd2', 15, ''),
                $opts['pwd_err']
        );

    echo "<tr>\n";
    echo "<td colspan=\"2\" />\n";
    echo "<input type=\"submit\" name=\"submit\" value=\"Register\" />";
    echo "</td>\n</tr>\n";

    echo "</tbody>\n";
    echo "</form>\n";
    echo "</table>\n";


}



?>